package com.novax.ex.activity.provider.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.novax.ex.activity.infrastructure.entity.MallOrderWinner;
import com.novax.ex.activity.open.api.inner.MallOrderWinnerInnerApi;
import com.novax.ex.activity.open.model.query.MallWinnerRecordQuery;
import com.novax.ex.activity.open.model.request.MallWinnerMemoRequest;
import com.novax.ex.activity.open.model.request.MallWinnerStatusRequest;
import com.novax.ex.activity.open.model.response.MallOrderWinnerMemosResponse;
import com.novax.ex.activity.open.model.response.MallWinnerRecordResponse;
import com.novax.ex.activity.provider.service.MallOrderWinnerMemosService;
import com.novax.ex.activity.provider.service.MallOrderWinnerService;
import com.novax.ex.common.constant.InternationalCode;
import com.novax.ex.common.constant.StatusCode;
import com.novax.ex.common.enums.MallOrderWinnerStatus;
import com.novax.ex.common.results.ReturnResult;
import com.novax.ex.common.util.CopyUtils;
import com.novax.ex.common.util.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.text.MessageFormat;
import java.util.*;

/**
 * Description: 用户中奖记录
 *
 * @author Deucalion@novax.email
 * @date 2023/2/23 17:29
 */
@Slf4j
@RestController
public class MallOrderWinnerController extends BaseActivityController implements MallOrderWinnerInnerApi {
    @Resource
    private MallOrderWinnerService mallOrderWinnerService;
    @Resource
    private MallOrderWinnerMemosService mallOrderWinnerMemosService;

    @Override
    public ReturnResult recordStatusChange(Long id, String language, Integer creatorType, Long adminId, Integer status) {
        if(null == id || null == status){
            return ReturnResult.fail(getInternational(language, InternationalCode.LACK_VALUE));
        }
        MallOrderWinner old = mallOrderWinnerService.find(id);
        if(null == old){
            return ReturnResult.fail(getInternational(language, InternationalCode.NOT_EXIST));
        }
        // 只能选2,3,4
        if(status < 2 || status > 4){
            return ReturnResult.fail(getInternational(language, InternationalCode.CANNOT_OPERATION));
        }
        // 修改为2
        if(MallOrderWinnerStatus.sendEntity.getStatus().equals(status)){
            if(!(MallOrderWinnerStatus.noSay.getStatus().equals(old.getStatus())
                    || MallOrderWinnerStatus.sendUSDT.getStatus().equals(old.getStatus()))){
                return ReturnResult.fail(getInternational(language, InternationalCode.CANNOT_OPERATION));
            }
        }
        // 修改为3
        if(MallOrderWinnerStatus.sendUSDT.getStatus().equals(status)){
            if(!(MallOrderWinnerStatus.noSay.getStatus().equals(old.getStatus())
                    || MallOrderWinnerStatus.sendEntity.getStatus().equals(old.getStatus()))) {
                return ReturnResult.fail(getInternational(language, InternationalCode.CANNOT_OPERATION));
            }
        }
        // 修改为4
        if(MallOrderWinnerStatus.success.getStatus().equals(status)){
            if(!(MallOrderWinnerStatus.sendEntity.getStatus().equals(old.getStatus())
                    || MallOrderWinnerStatus.sendUSDT.getStatus().equals(old.getStatus()))){
                return ReturnResult.fail(getInternational(language, InternationalCode.CANNOT_OPERATION));
            }
        }
        boolean result = mallOrderWinnerService.statusChange(id,old.getMallPrizeOrderId(),creatorType,adminId,status,old.getStatus());
        if (result) {
            return ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS));
        }
        return ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }

    @Override
    public ReturnResult<List<MallOrderWinnerMemosResponse>> memo(String language, Long mallOrderWinnerId) {
        Map<String, Object> param = new HashMap<>();
        param.put("mallOrderWinnerId",mallOrderWinnerId);
        List<MallOrderWinnerMemosResponse> list = mallOrderWinnerMemosService.findList(param);
        if(list.size() == 0){
            return ReturnResult.success(new ArrayList<>());
        }else{
            return ReturnResult.success(CopyUtils.copyList(list, MallOrderWinnerMemosResponse.class));
        }
    }

    @Override
    public ReturnResult memoChange(String language, MallWinnerMemoRequest request) {
        if(null == request.getId() || null == request.getMemo()){
            return ReturnResult.fail(getInternational(language, InternationalCode.LACK_VALUE));
        }
        if(request.getMemo().length() > 2048){
            String content = MessageFormat.format(getInternational(language, InternationalCode.OUT_OF_LENGTH_LIMIT), "memo","2048");
            return new ReturnResult<>(StatusCode.ERROR,content);
        }
        MallOrderWinner old = mallOrderWinnerService.find(request.getId());
        if(null == old){
            return ReturnResult.fail(getInternational(language, InternationalCode.NOT_EXIST));
        }
        MallOrderWinner entity = new MallOrderWinner();
        entity.setId(request.getId());
        entity.setMemo(request.getMemo());
        boolean result = mallOrderWinnerService.memoChange(entity,request.getCreatorType(),request.getAdminId());
        if (result) {
            return ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS));
        }
        return ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }

    @Override
    public ReturnResult<PageUtil<MallWinnerRecordResponse>> list(String language, MallWinnerRecordQuery query) {
        PageUtil<MallWinnerRecordResponse> pages = new PageUtil<>(query);
        Map<String, Object> condition = new HashMap<>();
        condition.put("query", query);
        Set<String> mallProductIds = new HashSet<>();
        if (StrUtil.isEmpty(query.getMallProductIds())) {
            condition.put("mallProductIds", null);
        } else {
            Collections.addAll(mallProductIds, query.getMallProductIds().split(","));
            condition.put("mallProductIds", mallProductIds);
        }
        pages.setCondition(condition);
        mallOrderWinnerService.findPage(pages,MallWinnerRecordResponse.class);
        return ReturnResult.success(pages);
    }

    @Override
    public ReturnResult<MallWinnerRecordResponse> detail(Long id, String language) {
        MallOrderWinner old = mallOrderWinnerService.find(id);
        if(null == old){
            return ReturnResult.success(getInternational(language, InternationalCode.NOT_EXIST));
        }
        MallWinnerRecordResponse response = BeanUtil.copyProperties(old, MallWinnerRecordResponse.class);
        return ReturnResult.success(getInternational(language, InternationalCode.QUERY_SUCCESS), response);
    }

    @Override
    public ReturnResult batchStatusChange(String language, MallWinnerStatusRequest request) {
        if(StrUtil.isEmpty(request.getIds()) || null == request.getStatus()){
            return ReturnResult.fail(getInternational(language, InternationalCode.LACK_VALUE));
        }
        String[] strArr = request.getIds().split(",");
        Set<String> ids = new HashSet<>();
        Collections.addAll(ids, strArr);
        if(ids.size() == 0){
            return ReturnResult.fail(getInternational(language, InternationalCode.LACK_VALUE));
        }
        if(ids.size() > 200){
            String content = MessageFormat.format(getInternational(language, InternationalCode.BATCH_OPERATION), "200");
            return ReturnResult.fail(content);
        }
        // 只能选2,3,4
        if(request.getStatus() < 2 || request.getStatus() > 4){
            return ReturnResult.fail(getInternational(language, InternationalCode.CANNOT_OPERATION));
        }
        List<MallOrderWinner> list = mallOrderWinnerService.findByIds(ids);
        for (MallOrderWinner orderWinner:list){
            // 修改为2
            if(MallOrderWinnerStatus.sendEntity.getStatus().equals(request.getStatus())){
                if(!(MallOrderWinnerStatus.noSay.getStatus().equals(orderWinner.getStatus())
                        || MallOrderWinnerStatus.sendUSDT.getStatus().equals(orderWinner.getStatus()))){
                    return ReturnResult.fail(getInternational(language, InternationalCode.CANNOT_OPERATION));
                }
            }
            // 修改为3
            if(MallOrderWinnerStatus.sendUSDT.getStatus().equals(request.getStatus())){
                if(!(MallOrderWinnerStatus.noSay.getStatus().equals(orderWinner.getStatus())
                        || MallOrderWinnerStatus.sendEntity.getStatus().equals(orderWinner.getStatus()))) {
                    return ReturnResult.fail(getInternational(language, InternationalCode.CANNOT_OPERATION));
                }
            }
            // 修改为4
            if(MallOrderWinnerStatus.success.getStatus().equals(request.getStatus())){
                if(!(MallOrderWinnerStatus.sendEntity.getStatus().equals(orderWinner.getStatus())
                        || MallOrderWinnerStatus.sendUSDT.getStatus().equals(orderWinner.getStatus()))){
                    return ReturnResult.fail(getInternational(language, InternationalCode.CANNOT_OPERATION));
                }
            }
        }
        // 状态为1只能改为2、3,状态为2、3只能改为4
        boolean result = mallOrderWinnerService.batchStatusChange(ids,request.getCreatorType(),request.getAdminId(),request.getStatus());
        if (result) {
            return ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS));
        }
        return ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }
}

